package com.santiao.fuel.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

/**
 *
 * @author LIUJING
 * 版本：2.0
 * 创建日期：2022/1/4 0004
 * 描述：default
 */
public class ShimmerLayout extends RelativeLayout {
    /* renamed from: a  reason: collision with root package name */
    public Paint f6584a;

    /* renamed from: b  reason: collision with root package name */
    public int f6585b;

    /* renamed from: c  reason: collision with root package name */
    public int f6586c;

    /* renamed from: d  reason: collision with root package name */
    public int f6587d;

    /* renamed from: e  reason: collision with root package name */
    public int f6588e;

    /* renamed from: g  reason: collision with root package name */
    public int f6590g;

    /* renamed from: h  reason: collision with root package name */
    public int f6591h;
    public boolean k;
    public AnimatorSet l;
    public ViewTreeObserver.OnPreDrawListener m;

    /* renamed from: f  reason: collision with root package name */
    public int f6589f = 0;
    public boolean i = false;
    public int j = 50;


    /* loaded from: classes.dex */
    public class a implements ViewTreeObserver.OnPreDrawListener {
        public a() {
        }

        @Override // android.view.ViewTreeObserver.OnPreDrawListener
        public boolean onPreDraw() {
            ShimmerLayout.this.getViewTreeObserver().removeOnPreDrawListener(this);
            ShimmerLayout.this.start();
            return true;
        }
    }

    /* loaded from: classes.dex */
    public class b implements ValueAnimator.AnimatorUpdateListener {
        public b() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ShimmerLayout.this.f6589f = ((Integer) valueAnimator.getAnimatedValue()).intValue();
            ShimmerLayout.this.invalidate();
        }
    }

    /* loaded from: classes.dex */
    public class c extends AnimatorListenerAdapter {
        public c() {
        }

        @Override
        // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationRepeat(Animator animator) {
            ShimmerLayout.this.f6589f = 0;
            ShimmerLayout.this.i = true;
        }
    }

    /* loaded from: classes.dex */
    public class d implements ValueAnimator.AnimatorUpdateListener {
        public d() {
        }

        @Override // android.animation.ValueAnimator.AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ShimmerLayout.this.f6584a.setAlpha(((Integer) valueAnimator.getAnimatedValue()).intValue());
        }
    }

    /* loaded from: classes.dex */
    public class e extends AnimatorListenerAdapter {
        public e() {
        }

        @Override
        // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
        public void onAnimationRepeat(Animator animator) {
            ShimmerLayout.this.f6584a.setAlpha(128);
        }
    }


    public ShimmerLayout(Context context) {
        super(context);
    }

    private void c() {
        this.f6590g = getWidth() / 2;
        this.f6591h = getHeight() / 2;
        this.f6585b = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        this.f6586c = measuredHeight;
        int max = (Math.max(this.f6585b, measuredHeight) / 2) + 20;
        this.f6587d = max;
        this.f6589f = 0;
        this.i = true;
        this.f6588e = max / 20;
    }

    private void d() {
        this.f6584a = new Paint(1);
        setWillNotDraw(false);
        this.f6584a.setColor(-1);
        this.f6584a.setAlpha(128);
    }

    @SuppressLint("WrongConstant")
    private AnimatorSet getShimmerAnimation() {
        AnimatorSet animatorSet = this.l;
        if (animatorSet != null) {
            return animatorSet;
        }
        ValueAnimator ofInt = ValueAnimator.ofInt(this.f6589f, this.f6587d);
        ofInt.setDuration(900L);
        ofInt.setRepeatMode(1);
        ofInt.setRepeatCount(-1);
        ofInt.setInterpolator(new LinearInterpolator());
        ofInt.addUpdateListener(new b());
        ofInt.addListener(new c());
        ValueAnimator ofInt2 = ValueAnimator.ofInt(128, 30);
        ofInt2.setDuration(900L);
        ofInt2.setRepeatMode(1);
        ofInt2.setRepeatCount(-1);
        ofInt2.setInterpolator(new LinearInterpolator());
        ofInt2.addUpdateListener(new d());
        ofInt2.addListener(new e());
        AnimatorSet animatorSet2 = new AnimatorSet();
        this.l = animatorSet2;
        animatorSet2.playTogether(ofInt, ofInt2);
        this.l.setDuration(900L);
        return this.l;
    }

    public void stop() {
        if (this.m != null) {
            getViewTreeObserver().removeOnPreDrawListener(this.m);
        }
        AnimatorSet animatorSet = this.l;
        if (animatorSet != null) {
            animatorSet.end();
            this.l.removeAllListeners();
            this.l.cancel();
        }
        this.l = null;
        this.i = false;
        postInvalidateDelayed((long) this.j);
        this.k = false;
    }

    @Override // android.view.View, android.view.ViewGroup
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (this.i) {
            canvas.save();
            canvas.drawCircle((float) this.f6590g, (float) this.f6591h, (float) this.f6589f, this.f6584a);
            canvas.restore();
        }
    }

    @Override // android.widget.RelativeLayout, android.view.View, android.view.ViewGroup
    public void onLayout(boolean z, int i, int i2, int i3, int i4) {
        super.onLayout(z, i, i2, i3, i4);
    }

    public void setRippleColor(int i) {
        this.f6584a.setColor(i);
    }

    public void start() {
        if (this.k) {
            return;
        }
        if (getWidth() == 0) {
            this.m = new a();
            getViewTreeObserver().addOnPreDrawListener(this.m);
            return;
        }
        c();
        getShimmerAnimation().start();
        this.k = true;
    }

    public ShimmerLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        d();
    }

    @TargetApi(11)
    public ShimmerLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        d();
    }

}
